/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_NET_HTTP_HTTP_REQUEST_H_
#define NODE_NET_HTTP_HTTP_REQUEST_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cassert>
#include <map>
#include <event2/http.h>
#include "node/macros.h"

namespace node {
    namespace http {
        class HttpResponse;

        class HttpRequest
        {
        public:
            HttpRequest(struct evhttp_request * request);
            ~HttpRequest(void);

            void Initialize(void);

            inline const char *get_uri(void) const {
                assert(request_ != NULL);
                return evhttp_request_get_uri(request_);
            }
            ::std::size_t get_input_size(void) const;
            int Read(char *buffer, int size);

            const ::std::map<const char*, const char *> &get_headers(void) const {
                return headers_;
            }

            const char *get_header(const char *name) {
                if (headers_.find(name) != headers_.end()) {
                    return headers_[name];
                }
                return NULL;
            }

            inline const char *get_host(void) const {
                assert(decoded_ != NULL);
                return evhttp_uri_get_host(decoded_);
            }
            inline const int get_port(void) const {
                assert(decoded_ != NULL);
                return evhttp_uri_get_port(decoded_);
            }
            inline const char *get_path(void) const {
                assert(decoded_ != NULL);
                return evhttp_uri_get_path(decoded_);
            }

            inline const char *get_query(void) const {
                assert(decoded_ != NULL);
                return evhttp_uri_get_query(decoded_);
            }

            inline bool IsParseOk(void) const {
                return parse_ok_;
            }

        private:
            friend class HttpResponse;

            struct evhttp_request *request_;
            int method_;
            ::std::map<const char*, const char *> headers_;
            struct evbuffer *input_;
            struct evhttp_uri *decoded_;

            bool parse_ok_;
            DISALLOW_COPY_AND_ASSIGN(HttpRequest);
        };

    }
}

#endif // NODE_NET_HTTP_HTTP_REQUEST_H_
